package com.online.taxi.order.implementation.mysql;

import com.online.taxi.order.implementation.LockKey;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author:jiangqikun
 * @Date:2022/8/21 14:12
 **/

public class MysqlLockUse {

    @Autowired
    private MysqlLock lock;

    ThreadLocal<LockKey> local = new ThreadLocal<>();

    public void use(String orderId, String driverId){
        // 生成 key
        LockKey key = LockKey.builder()
                .driverId(driverId)
                .orderId(orderId)
                .build();
        // 将 key 存放到 local
        local.set(key);
        // 将 local 传入 lock
        lock.setOrderLockThreadLocal(local);

        try {
            lock.lock();
            //执行业务代码


        }finally {
            lock.unlock();
        }

    }


}
